package org.saratogaschools.edd.firefighter.client.activity;

import org.saratogaschools.edd.firefighter.client.ClientFactory;
import org.saratogaschools.edd.firefighter.client.place.EnterNamePlace;
import org.saratogaschools.edd.firefighter.client.place.SelectDepartmentPlace;
import org.saratogaschools.edd.firefighter.client.util.AsyncPlaceChange;
import org.saratogaschools.edd.firefighter.client.view.EnterNameView;
import org.saratogaschools.edd.firefighter.shared.model.FireFighter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EnterNameActivity extends AbstractActivity implements  EnterNameView.Presenter {
	
	private ClientFactory clientFactory;

	public EnterNameActivity(EnterNamePlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		EnterNameView view = new EnterNameView();
		view.setPresenter(this);
		panel.setWidget(view);
	}
	
	@Override
	public void onNameEntered(String name) {
		final FireFighter registrationCandidate = new FireFighter();
		registrationCandidate.setName(name);
		GWT.runAsync(new AsyncPlaceChange(clientFactory, new SelectDepartmentPlace(registrationCandidate)));
	}

}
